package hot100;

public class getIntersectionNode {
    public mergeKLists.ListNode getIntersectionNode(mergeKLists.ListNode headA, mergeKLists.ListNode headB) {
        int la = 0;
        int lb = 0;
        mergeKLists.ListNode curA = headA;
        mergeKLists.ListNode curB = headB;
        while(curA != null) {
            la++;
            curA = curA.next;
        }
        while(curB != null) {
            lb++;
            curB = curB.next;
        }
        int len = 0;
        if(la < lb) {
            len = lb - la;
            while(len > 0) {
                headB = headB.next;
                len--;
            }
        }else {
            len = la - lb;
            while(len > 0) {
                headA = headA.next;
                len--;
            }
        }
        while(headA != null && headB != null) {
            if(headA == headB && headA.val == headB.val) {
                return headA;
            }
            headA = headA.next;
            headB = headB.next;
        }
        return null;

    }
}
